feat(preloader): add preloadOnce to preload a relationship only once #1078
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
Similar to PR #1052 by @RomainLanz
β Type of change
π Description
Added a preloadOnce method to the query building using the same logic as the loadOnce
In our use case we allow the client to preload field using a query parameter
![Screenshot 2024-12-14 at 23 30 25](https://private-user-images.githubusercontent.com/1884246/395821199-3036b4c4-5500-496d-8068-df5222d3289f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4Njg3NTIsIm5iZiI6MTczODg2ODQ1MiwicGF0aCI6Ii8xODg0MjQ2LzM5NTgyMTE5OS0zMDM2YjRjNC01NTAwLTQ5NmQtODA2OC1kZjUyMjJkMzI4OWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDZUMTkwMDUyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZGIwMjgzY2EzMjU1ZDA4MDYwODAyMmE0ZjJmYzk5OGRhMTgwZjBlYWVkNmQzMmNkNTlhZTRhOTBiNTJjZTdjYSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.49sGmR1FzvHqckycZVn79zIPcObN7FDUTLH86Jen2LY)
$populate: string[]
While doing so we sometimes also use preload in our services
![Screenshot 2024-12-14 at 23 32 53](https://private-user-images.githubusercontent.com/1884246/395821258-4623824c-a94a-4c80-aa5c-94dc348437a3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4Njg3NTIsIm5iZiI6MTczODg2ODQ1MiwicGF0aCI6Ii8xODg0MjQ2LzM5NTgyMTI1OC00NjIzODI0Yy1hOTRhLTRjODAtYWE1Yy05NGRjMzQ4NDM3YTMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDZUMTkwMDUyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NzFjZjEyYWI5NzM4MDFkYjE5MDhkMTMzNWE1N2QwMWY4YmMwMmJiZDhhZjU5NzBjMmI3NmNmNjhmY2FkYmFkYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.-qMtV5qsWUXcSrQbBC0t6EBmdgPpajpfEKOa4fBsCD0)
If the client request
find
with the following payloadWithout
.preloadOnce
query.preload("tenant", (subquery) => { subquery.preload('settings') })
query.preload("tenant")
and override the callbackfind
will return the data with thetenant
populated but without thetenant > settings
With
.preloadOnce
query.preload("tenant", (subquery) => { subquery.preload('settings') })
query.preloadOnce("tenant")
and nothing will happen as tenant is already queued for preloadingfind
will return the data with thetenant
populated and alsotenant > settings
populatedπ Checklist